<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Common;

use App\Controller\Admin\AdminController;
use App\Repositories\CommunityRepository;
use App\Request\StoreNameRequest;

class CommunityController extends AdminController
{
    public function all()
    {
        $street_id = $this->request->input('street_id',0);
        $where=[];
        if($street_id){
            $where=[
                'street_id'=>[
                    'field' => 'street_id',
                    'value' => $street_id
                ]
            ];
        }
        $return=CommunityRepository::getInstance()->all(['id','name','street_id'],$where);
        return $this->response->success($return);
    }

    public function list(){
        $rows = $this->request->input('rows',10);
        $name = $this->request->input('name',null);
        $search=[];
        if($name&&$name!=''){
            $search['name']=[
                'field' => 'name',
                'value' => $name,
                'method' => 'like'
            ];
        }
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'desc'
            ]
        ];
        $datas = CommunityRepository::getInstance()->getList('*',$search,['limit'=>$rows],$orderBy,'',['district','street']);
        return $this->response->success($datas);
    }

    public function detail(){
        $id = $this->request->input('id');
        $question = CommunityRepository::getInstance()->getById($id);
        return $this->response->success($question);
    }

    public function store(StoreNameRequest $request){
        $params = $request->all();
        $result = CommunityRepository::getInstance()->store($params);
        if($result['success']){
            return $this->response->success([
                'id' => $result['data']->id
            ]);
        }
        return $this->response->error(10000,$result['msg']);
    }

    public function delete(){
        $id = $this->request->input('id');
        $result =CommunityRepository::getInstance()->delete($id);
        if($result){
            return $this->response->success($result['data']);
        }
        return $this->response->error(10000,$result['msg']);
    }
}